﻿
#region Usings

using PolyGameEngine.Tools;


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

#endregion

namespace PolyGameEngine.MenuSystem
{
    public class PauseMenu
    {
        public delegate void PauseToMain();
        public event PauseToMain PauseToMainEvent;

        GameState gameState
        {
            get { return PhysicsGame.gamestate; }
            set { PhysicsGame.ChangeGameState(value); }
        }
        GameState PreviousGameState
        {
            get { return PhysicsGame.PreviousGameState; }
        }

        Viewport viewport;
        SpriteFont[] font;
        Texture2D background;
        Texture2D oshadow;


        float K = 0.3f;
        bool grow = true;
        ContentManager content;


        int currentItem = 0;
        string[] itemNames = { "Resume Game","Main Menu", "Exit Game"};


        public PauseMenu(ContentManager Content, Viewport Viewport, SpriteFont[] Font)
        {
            content = Content;
            viewport = Viewport;
            background = content.Load<Texture2D>("Menu/steel");
            oshadow = content.Load<Texture2D>("Menu/OuterShadow");
            font = Font;
        }



        public void update()
        {
            InputMan.Update();

            AudioMan.RollUpdate(0);

            if (!AudioMan.getPause())
            {
                AudioMan.PauseMusicOn();
            }

            if (InputMan.OnPress(Keys.Enter) && currentItem == 0)
            {
                AudioMan.PlayCue("Menu Select");
                if (PreviousGameState == GameState.Play)
                    if (AudioMan.getPause())
                        AudioMan.PauseMusicOff();
                gameState = PreviousGameState;
            }
            if (InputMan.OnPress(Keys.Enter) && currentItem == 1)
            {
                AudioMan.PlayCue("Menu Select");
                if (PauseToMainEvent != null)
                    PauseToMainEvent();
               gameState = GameState.Menu;
            }
            if (InputMan.OnPress(Keys.Enter) && currentItem == 2)
                gameState = GameState.Close;



            if (K >= 1f)
                grow = false;
            if (K <= 0.8f)
                grow = true;
            if (grow)
                K += 0.005F;
            else
                K -= 0.005f;

            if (InputMan.OnPress(Keys.S))
            {

                AudioMan.PlayCue("Menu Switch");
                if (++currentItem > itemNames.Length - 1)
                {
                    currentItem = 0;
                }
            }

            if (InputMan.OnPress(Keys.W))
            {

                AudioMan.PlayCue("Menu Switch");
                if (--currentItem < 0)
                {
                    currentItem = 0;
                }
            }

        }

        public void draw(SpriteBatch sb)
        {
            sb.Draw(background, new Rectangle(0, 0, background.Width, background.Height), Color.White);
            sb.Draw(oshadow, new Rectangle(0, 0, oshadow.Width, oshadow.Height), Color.White);

            Vector2 centreScreen = new Vector2(viewport.Width / 2 - (int)(viewport.Width * 0.1), viewport.Height / 2 - (int)(viewport.Width * 0.1));

            for (int i = 0; i <= itemNames.Length - 1; i++)
            {


                if (currentItem == i)
                    sb.DrawString(font[0], itemNames[i], centreScreen, Color.Red, 0, Vector2.Zero, K, SpriteEffects.None, 0);
                else
                    sb.DrawString(font[0], itemNames[i], centreScreen, Color.Black);


                centreScreen.Y += 35;



            }

            centreScreen.Y = viewport.Height;

        }



    }
}
